package com.xy.controller.order;

import com.xy.common.base.ApiBaseResponse;
import com.xy.common.base.BaseController;
import com.xy.entity.SysDict;
import com.xy.entity.express.ExpressFee;
import com.xy.entity.order.OrderDelivery;
import com.xy.entity.order.OrderMain;
import com.xy.entity.uc.Address;
import com.xy.entity.uc.Area;
import com.xy.enums.OrderStatus;
import com.xy.repository.AddressRepository;
import com.xy.repository.AreaRepository;
import com.xy.repository.SysDictRepository;
import com.xy.repository.order.ExpressFeeRepository;
import com.xy.repository.order.OrderDeliveryRepository;
import com.xy.repository.order.OrderMainRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Thinkpad on 2018/1/19.
 */
@Api(value = "用户controller", description = "用户操作")
@RestController
@RequestMapping(value = "/api/order")
public class OrderController extends BaseController {

    private static final String ORDER_CANNEL_REASON = "order_cannel_reason";

    private AreaRepository areaRepository;
    private AddressRepository addressRepository;
    private OrderMainRepository orderMainRepository;
    private ExpressFeeRepository expressFeeRepository;
    private SysDictRepository sysDictRepository;
    private OrderDeliveryRepository orderDeliveryRepository;

    @Autowired
    public OrderController(
            AreaRepository areaRepository,
            AddressRepository addressRepository,
            OrderMainRepository orderMainRepository,
            ExpressFeeRepository expressFeeRepository,
            SysDictRepository sysDictRepository,
            OrderDeliveryRepository orderDeliveryRepository
    ){
        this.areaRepository = areaRepository;
        this.addressRepository = addressRepository;
        this.orderMainRepository = orderMainRepository;
        this.expressFeeRepository = expressFeeRepository;
        this.sysDictRepository = sysDictRepository;
        this.orderDeliveryRepository = orderDeliveryRepository;
    }


    @ApiOperation(value = "根据收货地址获取信息", notes = "")
    @GetMapping(value = "/prepare/{areaUuid}")
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<?> stationArea(@PathVariable("areaUuid") String areaUuid) {
        Map resultMap = new HashMap<>();
        Area area = areaRepository.findOne(areaUuid);

        if (area != null) {
            resultMap.put("isOwnExpress",area);
            ExpressFee expressFee = expressFeeRepository.findByProvincesLike(area.getParentId());
            resultMap.put("expressFee",expressFee.getPrice());
        }

        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK,resultMap));
    }


    @ApiOperation(value = "确认下单", notes = "")
    @PostMapping(value = "/order")
    @ResponseStatus(HttpStatus.OK)
    @Transactional
    public ResponseEntity<?> putOrder(@Valid @RequestBody ApiOrderReqParam apiOrderReqParam) {

        Address address = addressRepository.findByUuidAndAndUserId(apiOrderReqParam.getAddressId(),userId);

        OrderMain orderMain = new OrderMain();
        orderMain.setUserId(userId);
        orderMain.setDeliveryTime(apiOrderReqParam.getDeliveryTime());
        orderMain.setOrderStatus(OrderStatus.WAITPAY.name());
        OrderMain orderMainNew = orderMainRepository.save(orderMain);

        OrderDelivery orderDelivery = new OrderDelivery();
        orderDelivery.setProvince(address.getProvince());
        orderDelivery.setCity(address.getCity());
        orderDelivery.setArea(address.getArea());
        orderDelivery.setVillage(address.getVillage());
        orderDelivery.setPostcode(address.getPostcode());
        orderDelivery.setReceiver(address.getReceiver());
        orderDelivery.setOrderMain(orderMainNew);
        OrderDelivery orderDeliveryNew = orderDeliveryRepository.save(orderDelivery);

        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK));
    }

    @ApiOperation(value = "获取订单列表",  notes = "The list is paginated. You can provide a page number (default 0) and a page size (default 20) more "
            + "?page=0&size=20&sort=b&sort=a,desc&sort=c,desc ")
    @GetMapping(value = "/order")
    @ResponseStatus(HttpStatus.OK)
    public Page<OrderMain> getOrder(@PageableDefault(value = 20, sort = {"gmtCreated"}, direction = Sort.Direction.DESC)
                                                Pageable pageable){
        initBaseInfo();
        Page<OrderMain> orderMainPage = orderMainRepository.findByUserId(userId,pageable);
        return orderMainPage;
    }

    @ApiOperation(value = "查询取消原因", notes = "")
    @GetMapping(value = "/reason")
    @ResponseStatus(HttpStatus.OK)
    public ResponseEntity<?> reason() {
        SysDict sysDict = sysDictRepository.findByCode(ORDER_CANNEL_REASON);
        List<SysDict> sysDictList = sysDictRepository.findAllByParentId(sysDict.getUuid());
        return ResponseEntity.ok(ApiBaseResponse.fromHttpStatus(HttpStatus.OK,sysDictList));
    }




}
